﻿using System;
using System.Windows;
using Douban.Api;
using Microsoft.Practices.Unity;
using Microsoft.Practices.Composite.Modularity;
using Microsoft.Practices.Composite.UnityExtensions;
using Douban.Core.Settings;
using Douban.Core.Menus;
using Microsoft.Practices.Composite.Presentation.Regions;
using Douban.Core.Regions;
using System.Windows.Controls;
using Douban.Core.HotKeys;
using Douban.Core.Messages;

namespace Douban
{
    public class DoubanBootstrapper : UnityBootstrapper
    {
        private bool _hasActivated = false;
        private IShellView _view;

        public IShellView View 
        { 
            get { return _view; } 
        }

        protected override DependencyObject CreateShell()
        {
            DoubanService doubanService = new DoubanService();
            doubanService.Run();

            Container.RegisterInstance(doubanService);

            IShellViewModel shellViewModel = Container.Resolve<IShellViewModel>();
            _view = shellViewModel.View;
            _view.Activated += Shell_Activated;
            _view.ShowView();

            return (DependencyObject)_view;
        }

        protected override void InitializeModules()
        {
            base.InitializeModules();
            _view.NotifyAllModulesLoaded();
        }

        protected override RegionAdapterMappings ConfigureRegionAdapterMappings()
        {
            RegionAdapterMappings mappings = base.ConfigureRegionAdapterMappings();
            mappings.RegisterMapping(typeof(Grid), this.Container.Resolve<GridRegionAdapter>());
            return mappings;
        }

        private void Shell_Activated(object sender, EventArgs e)
        {
            if (_hasActivated) return;

            _hasActivated = true;

            IShellView view = (IShellView)sender;

            DoubanService service = Container.Resolve<DoubanService>();
            var authorizeService = service.AuthorizationService;

            if (!authorizeService.IsAccessTokenAvailable)
            {
                Uri authorizeUrl = authorizeService.BeginAuthorize();
                view.PromptAuthorization(authorizeUrl);
                authorizeService.CompleteAuthorize();
            }

            view.Activated -= Shell_Activated;
        }

        protected override IModuleCatalog GetModuleCatalog()
        {
            return new ConfigurationModuleCatalog();
        }

        protected override void ConfigureContainer()
        {
            DoubanService doubanService = new DoubanService();
            doubanService.Run();

            Container
                .RegisterInstance(doubanService)
                .RegisterType<GridRegionAdapter>()
                .RegisterType<NotifyIconViewModel>()
                .RegisterType<ISettingManager, SettingManager>(new ContainerControlledLifetimeManager())
                .RegisterType<IMenuManager, MenuManager>(new ContainerControlledLifetimeManager())
                .RegisterType<IShellView, MainWindow>(new ContainerControlledLifetimeManager())
                .RegisterType<ISettingView, SettingWindow>()
                .RegisterType<ISettingViewModel, SettingViewModel>()
                .RegisterType<IShellViewModel, ShellViewModel>(new ContainerControlledLifetimeManager())
                .RegisterType<IMessageStack, MessageStack>(new ContainerControlledLifetimeManager());

            base.ConfigureContainer();
        }
    }
}
